<?php 
class TypeController extends  CommonController{
	private $model;
	public function __init(){
		parent::__init();
		$this->model=K("Type");
		
	}
	
	
	public function index(){
		$data=$this->model->all();
		$this->assign("data",$data);
		$this->display();
	}
	
	
	/**
	 * 添加类型
	 */
	public function add(){
		if(IS_POST){
			if(!$this->model->addType()) $this->error($this->model->error);
			$this->success('添加成功');
		}
		$this->display();
	}
	/**
	 * 修改类型
	 */
	public function edit(){
	    if(IS_POST){
			if(!$this->model->editType()) $this->error($this->model->error);
			$this->success('修改成功',U("index"));
		}
		//获得旧数据
		$tid=Q("get.tid","","intval");
		$oldData=$this->model->where("tid={$tid}")->find();
		$this->assign('oldData',$oldData);
		
		$this->display();
	}
	/**
	 * 删除类型
	 */
	public function del(){
		$tid=Q("get.tid","","intval");
		
		$cate= M("category")->where("shop_type_tid={$tid}")->find();
		if($cate){
			$this->error("请先删除分类");
		}
        K("TypeAttr")->where("shop_type_tid={$tid}")->delete();
		K("Category")->where("shop_type_tid={$tid}")->delete();
		$this->model->where("tid={$tid}")->delete();

		$this->success('删除成功',U('index'));
	}
	
	
}


 ?>